﻿using System;
using System.Text;
using System.Web.UI.WebControls;
using VitCode.Castle.Helper;
using VitCode.Web.UI;

namespace VitCode.BaseDataPageCreator
{
	/// <summary>
	/// QueryHelper 的摘要說明。
	/// </summary>
	public class QueryHelper
	{
		/// <summary>
		/// 獲取簡單查詢條件
		/// </summary>
		/// <param name="isDelete">是否已刪除</param>
		/// <param name="conditionSelectedValue">條件</param>
		/// <param name="queryText">值</param>
		/// <param name="isAmount">是否完全匹配查詢</param>
		/// <param name="logicOperator">條件開頭邏輯運算符</param>
		/// <returns>簡單查詢條件SQL語句</returns>
		public static string GetQueryCondition(bool isDelete,string conditionSelectedValue,string queryText, bool isAmount, string logicOperator)
		{
			int iIsDelete = Convert.ToInt32(isDelete);
			string result = logicOperator+" IsDelete = " + iIsDelete;
			if (isAmount)
			{
				if (queryText.Length == 0)
					result = logicOperator+" IsDelete = " + iIsDelete + " and (" + conditionSelectedValue + " = '" + queryText +
						"' or " + conditionSelectedValue + " is null ) ";
				else
					result = logicOperator+" IsDelete = " + iIsDelete + " and " + conditionSelectedValue + " = '" + queryText +
						"' ";
			}
			else
			{
				if (queryText.Length > 0)
					result = logicOperator+" IsDelete = " + iIsDelete + " and " + conditionSelectedValue + " like '%" + queryText +
						"%' ";
			}
			return result;
		}

		public static string GetQueryCondition(bool isDelete,string conditionSelectedValue,string queryText, bool isAmount)
		{
			return GetQueryCondition(isDelete,conditionSelectedValue,queryText,isAmount,"where");
		}

		public static string GetQueryCondition(string conditionSelectedValue,string queryText, bool isAmount)
		{
			return GetQueryCondition(conditionSelectedValue,queryText,isAmount,"where");
		}


		/// <summary>
		/// 針對日期輸入有問題的重寫
		/// </summary>
		/// <param name="conditionSelectedValue">條件</param>
		/// <param name="queryText">值</param>
		/// <param name="isAmount">是否完全匹配查詢</param>
		/// <param name="isYear">是否針對日期處理</param>
		/// <returns>簡單查詢條件SQL語句</returns>
		public static string GetQueryCondition(string conditionSelectedValue,string queryText, bool isAmount, bool isYear)
		{
			return GetQueryCondition(conditionSelectedValue,queryText,isAmount,"where",isYear);
		}
		public static string GetQueryCondition(string conditionSelectedValue,string queryText, bool isAmount,string logicOperator,bool isYear)
		{
			string result = "";
			if (isAmount)
			{
				if (queryText.Length == 0)
					result = logicOperator+" (" + conditionSelectedValue + " = '" + queryText +
						"' or " + conditionSelectedValue + " is null ) ";
				else if( isYear && conditionSelectedValue.ToLower()=="year"   )
				{
					//針對日期處理 code by catcn 2009-2-25
					result = logicOperator+" " + conditionSelectedValue + " = " +
						" case  when (isdate('" +
						queryText +"')=1 ) then  '" +
						queryText +"' else '1909' end ";
				}
				else
					result = logicOperator+" " + conditionSelectedValue + " = '" + queryText +
						"' ";
			}
			else
			{
				if (queryText.Length > 0)
					result = logicOperator+" " + conditionSelectedValue + " like '%" + queryText +
						"%' ";
			}
			return result;
		}

		/// <summary>
		/// 獲取簡單查詢條件
		/// </summary>
		/// <param name="conditionSelectedValue">條件</param>
		/// <param name="queryText">值</param>
		/// <param name="isAmount">是否完全匹配查詢</param>
		/// <param name="logicOperator">條件開頭邏輯運算符</param>
		/// <returns>簡單查詢條件SQL語句</returns>
		public static string GetQueryCondition(string conditionSelectedValue,string queryText, bool isAmount,string logicOperator)
		{
			string result = "";
			if (isAmount)
			{
				if (queryText.Trim().Length == 0)
					result = logicOperator+" (" + conditionSelectedValue + " = '" + queryText.Trim() +
						"' or " + conditionSelectedValue + " is null ) ";
				else
				{
                    //if(conditionSelectedValue.Contains("日期") || conditionSelectedValue.Contains("Date"))
                    //{
                    //    try
                    //    {
                    //        string queryValue = Convert.ToDateTime(queryText.Trim()).ToString("yyyy-MM-dd");
                    //          result = logicOperator + " convert(char(10)," + conditionSelectedValue.Trim() + ",120) = '" + queryValue +
                    //   "' "; 
                    //    }
                    //    catch (Exception)
                    //    {
                    //        Alert.MessageBox("輸入的查詢的日期格式非法");
                    //    }
                      
                    //}
                    //else
                    //{
                      result = logicOperator + " " + conditionSelectedValue.Trim() + " = '" + queryText.Trim() +
						"' ";   
                    //}
				   
				}
                    
			}
			else
			{
				if (queryText.Length > 0)
                    //if (conditionSelectedValue.Contains("日期") || conditionSelectedValue.Contains("Date") || conditionSelectedValue.Contains("時間"))
                    //{
                    //     try
                    //    {
                    //        string queryValue = Convert.ToDateTime(queryText.Trim()).ToString("yyyy-MM-dd");
                    //          result = logicOperator + " convert(char(10)," + conditionSelectedValue.Trim() + ",120) like '%" + queryValue + "%'";
                    //    }
                    //    catch (Exception)
                    //    {
                    //        Alert.MessageBox("輸入的查詢的日期格式非法");
                            
                    //    }
                       
                    //}
                    //else
                    //{
                     result = logicOperator + " " + conditionSelectedValue.Trim() + " like '%" + queryText.Trim() +
						"%' ";   
                    //}
                    
			}
			return result;
		}

		/// <summary>
		/// 設置簡單查詢的條件
		/// </summary>
		/// <param name="pg">列表</param>
		/// <param name="ddlCondition">查詢選項</param>
		public static void SetDdlCondition(DropDownList ddlCondition,PaginationGrid pg)
		{
			for (int i = 0; i < pg.Columns.Count; i++)
			{
				if (pg.Columns[i] is PaginationGridBoundColumn)
				{
					PaginationGridBoundColumn bColumn = (PaginationGridBoundColumn) pg.Columns[i];
					ddlCondition.Items.Add(new ListItem(bColumn.HeaderText, bColumn.DataField));
				}
			}
		}

		/// <summary>
		/// 將一個字條串按指定的字元和每行的最小長度加入換行符
		/// </summary>
		/// <param name="source">原字串</param>
		/// <param name="fenGeFu">分隔符號</param>
		/// <param name="lineSize">每行的最小長度</param>
		/// <returns>加入換行符的字條串</returns>
		public static string ToRowsString(string source,string fenGeFu,int lineSize)
		{
			int tempIndex = 0;
			StringBuilder sb = new StringBuilder();
			while(tempIndex < source.Length)
			{
				if((tempIndex + lineSize + 1) >= source.Length)
				{
					if(tempIndex < source.Length)
						sb.Append(source.Substring(tempIndex));
					break;
				}
				int rowLastIndex = source.IndexOf(fenGeFu, tempIndex + lineSize);
				if(rowLastIndex>-1)
				{
					sb.Append(source.Substring(tempIndex, rowLastIndex - tempIndex)+" \r\n ");
					tempIndex = rowLastIndex + 1;
				}
				else
				{
					sb.Append(source.Substring(tempIndex));
					tempIndex = source.Length;
				}
			}
			return sb.ToString();
		}

	}
}
